package com.egg.auth.controller.api;

import com.egg.auth.entity.user.User;
import com.egg.auth.model.request.api.UserPatch;
import com.egg.auth.model.response.api.UserInfo;
import com.egg.auth.service.user.IUserService;
import com.egg.common.context.util.LocalThreadContextUtils;
import com.egg.common.core.model.controller.ApiController;
import com.egg.common.core.model.response.ResponseData;
import com.egg.common.util.JsonUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@Slf4j
@Api(tags = ApiController.API_PREFIX + "用户中心")
@RestController
@RequestMapping("/user")
public class ApiUserController extends ApiController {


    @Autowired
    private IUserService userService;


    @ApiOperation(value = "当前用户详情")
    @GetMapping("/info")
    public ResponseData<UserInfo> currentUserInfoGet() {
        User user = userService.baseGetById(LocalThreadContextUtils.getUserId());
        UserInfo userInfo = JsonUtil.objToObj(user, UserInfo.class);
        return ResponseData.success(userInfo);
    }

    @ApiOperation(value = "用户部分信息修改")
    @PatchMapping("/info/current")
    public ResponseData<Long> userPatch(@RequestBody UserPatch param) {
        User user = userService.baseGetById(LocalThreadContextUtils.getUserId());
        User save = new User();
        save.setId(user.getId());
        save.setNickname(param.getNickname());
        save.setAvatar(param.getAvatar());
        userService.baseEdit(save);
        return ResponseData.success(user.getId());
    }

}
